package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.service.impl.DishServiceImp;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品相关接口")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Qualifier("docket1")
    @Autowired
    private Docket docket1;

    /**
     * 菜品分页查询接口
     * @param dishPageQueryDTO
     * @return
     */
    @ApiOperation("菜品分页查询接口")
    @GetMapping("/page")
    public Result<PageResult> PageDish(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品分页查询");
        PageResult pageResult = dishService.PageDish(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 菜品批量删除接口
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("菜品批量删除接口")
    public Result deleteBatch(@RequestParam List<Long> ids){
        log.info("菜品批量删除{}",ids);
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        dishService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result GetDishById(@PathVariable Long id){
        log.info("根据菜品id查询菜品{}",id);
        DishVO dishVO = dishService.GetDishById(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品信息
     * @param dishVO
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品信息")
    public Result UpdateDish(@RequestBody DishDTO dishDTO){
        log.info("修改菜品{}",dishDTO);
        Long categoryId = dishDTO.getCategoryId();
        String key = "dish_"+categoryId;
        redisTemplate.delete(key);
        dishService.UpdateDish(dishDTO);
        return Result.success();
    }

    /**
     * 起售/停售接口
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("起售/停售接口")
    public Result IsSale(@PathVariable Integer status,
                         @RequestParam Long id){
        log.info("停售或起售{}{}",status,id);
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        dishService.IsSale(status,id);
        return Result.success("修改成功");
    }

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品接口")
    public Result DishAdd(@RequestBody DishDTO dishDTO){
        log.info("新增菜品{}",dishDTO);
        String key = "dish_"+dishDTO.getCategoryId();
        redisTemplate.delete(key);
        dishService.DishAdd(dishDTO);
        return Result.success("添加成功");
    }

    /**
     * 根据分类id查询菜品
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> DishByCategoryId(Long categoryId){
        log.info("根据分类id查询菜品{}",categoryId);
        List<Dish> DishList = dishService.GetDishByCategoryId(categoryId);
        return Result.success(DishList);
    }
}
